/* 
 * Based on Lowe, "Distinctive Image Features form Scale-Invariant Keypoints",
 * International Journal of Computer Vision, 2004
 */

#include<math.h>
#include "sc.h"

#define SLICES  8
#define OCTAVES 1
#define MEASURE
typedef float descriptor_t[128];

typedef struct
{
  int x, y;
} point_t;

PixelToPixelOp()
normalize(in stream descriptor_t *i,
          out stream descriptor_t *o)
{
  float total=0;
  int d;
  
  for (d=0; d < 128; d++)
    total = total + (*i)[d];

  for (d=0; d < 128; d++)
  {
    /* p16, "... the vector is normalized to unit length." */
    (*o)[d] = (*i)[d] / total;

    /* p16, "... thresholding the values in the unit feature vector to each be no
     * larger than 0.2 ... " */
    if ((*o)[d] > 0.2)
      (*o)[d] = 0.2;
  }
  
  total = 0;
  for (d=0; d < 128; d++)
    total += (*i)[d];

  for (d=0; d < 128; d++)
  {
    /* p16, "... and then renormalizing to unit length." */
    (*o)[d] = (*i)[d] / total;
  }
}

PixelToPixelOp()
print(in stream descriptor_t *i)
{
  int d;

  printf("{ ");
  for (d=0; d < 128; d++)
    printf("%f ", (*i)[d]);
  printf("}\n");
}


MAINTYPE MAIN(int argc, char **argv)
{
  int o, s, sl;
  double k = pow(2, 1.0/SLICES), sigma;
  STREAM image, gss[OCTAVES][SLICES+3], dog[OCTAVES][SLICES+2], keypoints, ovl, disp;
  scInit(argc, argv);
  _allmulti(1);
#ifndef MEASURE
  capture(&image);
#else
  generate(scint(1024), &image);
#endif
  for (o=0; o < OCTAVES; o++)
  {
   
    for (sl=1; sl < SLICES+1; sl++)
    {
      STREAM p, m, t, extrema, hthr, orientation;
    
      s = sl;

     
      /* Normalize to unit length */
      normalize(&keypoints, &keypoints);
#ifndef MEASURE
      /* Some output */
      print(&keypoints);
#else
      discard(&keypoints);
#endif
      _finalize(&keypoints);
      _finalize(&orientation);
      _finalize(&m);
      _finalize(&t);
      _finalize(&hthr);
      _finalize(&p);
      _finalize(&extrema);
    }

    for (s=0; s < SLICES+2; s++)
      _finalize(&dog[o][s]);

    /* Don't need the gaussian scalespace anymore */
    for (s=0; s < SLICES+3; s++)
      _finalize(&gss[o][s]);
  }

  _finalize(&image);

  return scExit();
}
